package org.jeecg.modules.htxincailiao.ability.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilityDemandOffer;
import org.jeecg.modules.htxincailiao.ability.service.IHTAbilityDemandOfferService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.htxincailiao.account.entity.HTMerchant;
import org.jeecg.modules.htxincailiao.account.entity.HTPlatformUser;
import org.jeecg.modules.htxincailiao.account.service.IHTAccountService;
import org.jeecg.modules.htxincailiao.account.service.IHTMerchantService;
import org.jeecg.modules.htxincailiao.address.entity.HTShopAddress;
import org.jeecg.modules.htxincailiao.address.service.IHTShopAddressService;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilityDemand;
import org.jeecg.modules.htxincailiao.ability.service.IHTAbilityDemandService;
import org.jeecg.modules.htxincailiao.shop.entity.HTShop;
import org.jeecg.modules.htxincailiao.shop.service.IHTShopService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.permissions.HTRolePermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: ht_ability_demand_offer
 * @Author: jeecg-boot
 * @Date:   2021-07-05
 * @Version: V1.0
 */
@Api(tags="ht_ability_demand_offer")
@RestController
@RequestMapping("/ability/hTAbilityDemandOffer")
@Slf4j
public class HTAbilityDemandOfferController extends JeecgController<HTAbilityDemandOffer, IHTAbilityDemandOfferService> {
	@Resource
	private IHTAbilityDemandOfferService hTAbilityDemandOfferService;


	 @Resource
	 private IHTAbilityDemandService abilityDemandService;

	 @Resource
	 private IHTAccountService accountService;

	 @Resource
	 private IHTShopService shopService;

	 @Resource
	 private IHTShopAddressService shopAddressService;
	
	/**
	 * 分页列表查询
	 *
	 * @param hTAbilityDemandOffer
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_ability_demand_offer-分页列表查询")
	@ApiOperation(value="ht_ability_demand_offer-分页列表查询", notes="ht_ability_demand_offer-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HTAbilityDemandOffer hTAbilityDemandOffer,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		JSONObject obj = HTRolePermission.GetShopPermissionInfo(req);
		if (obj.containsKey("shopId") && StringUtils.isNotBlank(obj.getString("shopId"))) {
			hTAbilityDemandOffer.setShopId(obj.getString("shopId"));
		}
		QueryWrapper<HTAbilityDemandOffer> queryWrapper = QueryGenerator.initQueryWrapper(hTAbilityDemandOffer, req.getParameterMap());
		Page<HTAbilityDemandOffer> page = new Page<HTAbilityDemandOffer>(pageNo, pageSize);
		IPage<HTAbilityDemandOffer> pageList = hTAbilityDemandOfferService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param hTAbilityDemandOffer
	 * @return
	 */
	@AutoLog(value = "ht_ability_demand_offer-添加")
	@ApiOperation(value="ht_ability_demand_offer-添加", notes="ht_ability_demand_offer-添加")
	@PostMapping(value = "/add")
	public Result<?> add(HttpServletRequest request, @RequestBody HTAbilityDemandOffer hTAbilityDemandOffer) {
		//判断报价者是否登录
		String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		if (oConvertUtils.isEmpty(token)) {
			return Result.error("报价用户未登录！");
		}

		//获取报价者用户信息, 更新报价者ID
		String username = JwtUtil.getUsername(token);
		HTPlatformUser offerUser = accountService.queryFrontendUserByUsername(username);
		if (offerUser != null) {
			hTAbilityDemandOffer.setOfferUserId(offerUser.getId());
			hTAbilityDemandOffer.setDemandUserId(offerUser.getId());
		}

		// 当前专利需求已报价实体 findAll
		// 同一个报价者只能报价一次
		List<HTAbilityDemandOffer> list = hTAbilityDemandOfferService.queryAbilityDemandOfferBy(hTAbilityDemandOffer.getAbilityDemandId());
		List<HTAbilityDemandOffer> offerList = list.stream( ).filter(offer -> offer.getOfferUserId().equals(hTAbilityDemandOffer.getOfferUserId())).collect(Collectors.toList());
		if (!offerList.isEmpty()) {
			return Result.error("不能重复报价");
		}

		//自己不能报价自己发布的需求
		if(offerUser != null && offerUser.getId().equals(hTAbilityDemandOffer.getShopId())){
			return Result.error("自己不能报价自己发布的需求");
		}

		//HTShopAddress shopAddress = shopAddressService.getById(shop.getShopAddressId());
		//
		//hTAbilityDemandOffer.setDemandUserId(shop.getSellerId());
		//if(oConvertUtils.isNotEmpty(shopAddress) && oConvertUtils.isNotEmpty(shopAddress.getPhoneNumber())) {
		//	hTAbilityDemandOffer.setDemandPhoneNumber(shopAddress.getPhoneNumber());
		//}
		hTAbilityDemandOffer.setCategoryId("1");
		boolean succeed =hTAbilityDemandOfferService.save(hTAbilityDemandOffer);
		//更新报价企业数
		if (succeed){
			List<HTAbilityDemandOffer> lastList = hTAbilityDemandOfferService.queryAbilityDemandOfferBy(hTAbilityDemandOffer.getAbilityDemandId());
			HTAbilityDemand demand = abilityDemandService.getById(hTAbilityDemandOffer.getAbilityDemandId());
			demand.setOfferEnterpriseCount(lastList.size());
			abilityDemandService.updateById(demand);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param hTAbilityDemandOffer
	 * @return
	 */
	@AutoLog(value = "ht_ability_demand_offer-编辑")
	@ApiOperation(value="ht_ability_demand_offer-编辑", notes="ht_ability_demand_offer-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HTAbilityDemandOffer hTAbilityDemandOffer) {
		hTAbilityDemandOfferService.updateById(hTAbilityDemandOffer);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_ability_demand_offer-通过id删除")
	@ApiOperation(value="ht_ability_demand_offer-通过id删除", notes="ht_ability_demand_offer-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hTAbilityDemandOfferService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ht_ability_demand_offer-批量删除")
	@ApiOperation(value="ht_ability_demand_offer-批量删除", notes="ht_ability_demand_offer-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hTAbilityDemandOfferService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_ability_demand_offer-通过id查询")
	@ApiOperation(value="ht_ability_demand_offer-通过id查询", notes="ht_ability_demand_offer-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HTAbilityDemandOffer hTAbilityDemandOffer = hTAbilityDemandOfferService.getById(id);
		if(hTAbilityDemandOffer==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hTAbilityDemandOffer);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hTAbilityDemandOffer
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTAbilityDemandOffer hTAbilityDemandOffer) {
        return super.exportXls(request, hTAbilityDemandOffer, HTAbilityDemandOffer.class, "ht_ability_demand_offer");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTAbilityDemandOffer.class);
    }

}
